package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.*;
import cn.wzut.edut.dao.pojo.table.*;
import cn.wzut.edut.service.SysService;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.LinkedList;
import java.util.List;


/**
 * 用户功能管理
 */
@Repository
public class User {
    @Autowired
    private PtyStuMapper ptyStuMapper;
    @Autowired
    private PtyTeacherMapper ptyTeacherMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleRelMapper sysUserRoleRelMapper;
    @Autowired
    private Information information;
    @Autowired
    private SysInformationMapper sysInformationMapper;
//    /**
//     * 学生登陆
//     * 输入学号
//     * @return hash后的密码
//     */
//    public String stuLogin(String stu_code){
//        /**
//         * 构造条件q1和q2
//         * q1：按照学号来查询用户id
//         * q2：找用户id来查找哈希后的密码
//         */
//        QueryWrapper<PtyStu> q1 = new QueryWrapper<>();
//        q1.select("user_id").in("stu_code",stu_code);
//        QueryWrapper<SysUser> q2 = new QueryWrapper<>();
//        q2.select("hash_password").in("id",ptyStuMapper.selectOne(q1).getUserId());
//        return sysUserMapper.selectOne(q2).getHashPassword();
//    }
//
//    /**
//     * 教师登陆
//     * 输入教工号
//     * @return hash后的密码
//     */
//    public String tcLogin(String teacher_code){
//        /**
//         * 构造条件q1和q2
//         * q1：按照教工号来查询用户id
//         * q2：找用户id来查找哈希后的密码
//         */
//        QueryWrapper<PtyTeacher> q1 = new QueryWrapper<>();
//        q1.select("user_id").in("teacher_code",teacher_code);
//        QueryWrapper<SysUser> q2 = new QueryWrapper<>();
//        q2.select("hash_password").in("id",ptyTeacherMapper.selectOne(q1).getUserId());
//        return sysUserMapper.selectOne(q2).getHashPassword();
//    }

    /**
     * 登陆
     * 输入用户名
     * @return hash后的密码
     */
    public String userLogin(String user_name){
        /**
         * 构造条件q1
         * q1：找用户名来查找哈希后的密码
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.select("hash_password").in("user_name",user_name).in("del_flag","1");
        return sysUserMapper.selectOne(q1).getHashPassword();
    }
    /**
     * 查询用户
     * 输入用户名
     * @return SysUser
     */
    public SysUser selectUser(String user_name){
        /**
         * 构造条件q1
         * q1：找用户名来查找
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.in("user_name",user_name);
        return sysUserMapper.selectOne(q1);
    }
    /**
     * 按真实姓名查询用户
     * 输入真实姓名
     * @return SysUser
     */
    public List<SysUser> selectUserInRealName(String real_name){
        /**
         * 构造条件q1
         * q1：按真实姓名来查找
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.in("real_name",real_name);
        return sysUserMapper.selectList(q1);
    }
    /**
     * 按真实姓名查询用户
     * 分页
     * 传入需要的页码current和每页显示的数量size
     * 输入真实姓名
     * @return SysUser
     */
    public List<SysUser> selectUserInRealNamePage(String real_name,long current,long size){
        /**
         * 构造条件q1
         * q1：找用户名来查找
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.in("real_name",real_name);
        return sysUserMapper.selectPage(new Page<SysUser>(current,size),q1).getRecords();
    }
    /**
     * 登陆
     * 输入用户id
     * @return SysUser
     */
    public SysUser selectUserInId(String user_id){
        /**
         * 构造条件q1
         * q1：找用户id来查找
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.in("id",user_id);
        SysUser sysUser =  sysUserMapper.selectOne(q1);
        System.out.println(sysUser);
        return sysUser;
    }
    /**
     * 按用户id查找用户角色表
     */
    public List<SysUserRoleRel> selectSysUserRoleRel(String id){
        /**
         * queryWrapper：按用户id，查找用户角色关系表
         */
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",id);
        return sysUserRoleRelMapper.selectList(queryWrapper);
    }
    /**
     * 按用户id查找用户角色表,分页
     *  传入需要的页码current和每页显示的数量size
     */
    public List<SysUserRoleRel> selectSysUserRoleRelPage(String id,long current,long size){
        /**
         * queryWrapper：按用户id，查找用户角色关系表
         */
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",id);
        return sysUserRoleRelMapper.selectPage(new Page<SysUserRoleRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * 按用户id查找用户角色表,仅返回角色列表
     * @return
     */
    public List<String> selectSysUserOnlyRole(String id){
        /**
         * queryWrapper：按用户id，查找用户角色关系表
         */
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("role_id").in("user_id",id);
        List<String> list = new LinkedList<>();
        List<SysUserRoleRel> list1 = sysUserRoleRelMapper.selectList(queryWrapper);
        for(int i = 0;i<list1.size();i++){
            list.add(list1.get(i).getRoleId());
        }
        return list;
    }
    /**
     * 按用户id查找用户角色表,仅返回角色列表,分页
     * 传入需要的页码current和每页显示的数量size
     * @return
     */
    public List<String> selectSysUserOnlyRolePage(String id,long current,long size){
        /**
         * queryWrapper：按用户id，查找用户角色关系表
         */
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("role_id").in("user_id",id);
        List<String> list = new LinkedList<>();
        List<SysUserRoleRel> list1 = sysUserRoleRelMapper.selectPage(new Page<SysUserRoleRel>(current,size),queryWrapper).getRecords();
        for(int i = 0;i<list1.size();i++){
            list.add(list1.get(i).getRoleId());
        }
        return list;
    }
    /**
     * 按用户id删除用户角色表
     */
    public boolean delSysUserRoleRel(String id){
        /**
         * queryWrapper：按用户id，删除用户角色关系表
         */
//        try {
            QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("user_id",id);
            sysUserRoleRelMapper.delete(queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }
    /**
     * 删除用户表
     */
    public boolean delSysUser(String id){
        /**
         * queryWrapper：按用户id，删除用户信息
         */
//        try {
                delSysUserRoleRel(id);

//              QueryWrapper<PtyStu> q1 = new QueryWrapper<>();
//              q1.in("user_id", id);
//              ptyStuMapper.delete(q1);
            delInformation(id);
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",id);
            sysUserMapper.delete(queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }


    @Autowired
    private PtyClassMapper ptyClassMapper;
    @Autowired
    private PtyCollegeMapper ptyCollegeMapper;
    /**
     * 初始化用的班级
     * @return
     */
    public boolean initPtyClass(){
//        try {
            ptyClassMapper.insert(new PtyClass("0","NoClass"));
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }
    /**
     * 增加信息
     */
    public boolean insertInformation(SysInformation sysInformation){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysInformation.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysInformationMapper.insert(sysInformation);
        return true;
    }
    /**
     * 初始化用的学院
     * @return
     */
    public boolean initPtyCollege(){
//        try {
            ptyCollegeMapper.insert(new PtyCollege("0","NoCollege"));
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    /**
     * 更新用户角色
     */
    public boolean updateSysUserRoleRel(SysUserRoleRel sysUserRoleRel){
//        try {
            QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in(sysUserRoleRel.getId());
            sysUserRoleRelMapper.update(sysUserRoleRel,queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }
    /**
     * 查询用户数
     */
    public long countSysUser(){
        return sysUserMapper.selectCount(null);
    }
    /**
     * 查询所有用户
     */
    public List<SysUser> selectAllSysUser(){
        return sysUserMapper.selectList(null);
    }
//    @Autowired
//    private Role rroollee;
//    /**
//     * 查询所有用户
//     */
//    public List<UserInf> selectAllSysUserRoleName(){
//        List<UserInf> list = new LinkedList<>();
//        List<SysUser> users = sysUserMapper.selectList(null);
//        for (int i = 0;i<users.size();i++){
//            list.add(new UserInf(users.get(i).getId(),users.get(i).getUserName(),users.get(i).getRealName(),users.get(i).getHashPassword(),rroollee.selectRoles(users.get(i).getId())));
//        }
//        return list;
//    }
//    /**
//     * 查询所有用户，分页
//     *     传入需要的页码current和每页显示的数量size
//     */
//    public List<UserInf> selectAllSysUserRoleNamePage(long current,long size){
//        List<UserInf> list = new LinkedList<>();
//        List<SysUser> users = sysUserMapper.selectPage(new Page<SysUser>(current,size),null).getRecords();
//        for (SysUser user : users) {
//            list.add(new UserInf(user.getId(), user.getUserName(), user.getRealName(), user.getHashPassword(), rroollee.selectRoles(user.getId())));
//        }
//        return list;
//    }

    /**
     * 查询所有用户角色表
     */
    public List<SysUserRoleRel> selectAllSysUserRoleRel(){
        return sysUserRoleRelMapper.selectList(null);
    }

    /**
     * 根据用户id查询信息
     */
    public SysInformation selectInformation(String userid){
        QueryWrapper<SysInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userid);
        return sysInformationMapper.selectOne(queryWrapper);
    }
    /**
     * 更新信息
     */
    public boolean updateInformation(SysInformation sysInformation){
        QueryWrapper<SysInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",sysInformation.getId());
        sysInformationMapper.update(sysInformation,queryWrapper);
        return true;
    }
    /**
     * 更改用户的个人信息
     * @param sysInformation
     * @return
     */
    public boolean updateUserInformation(SysInformation sysInformation){
        QueryWrapper<SysInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",sysInformation.getId());
        sysInformationMapper.update(sysInformation,queryWrapper);
        return true;
    }
    /**
     * 根据用户id删除信息
     */
    public boolean delInformation(String userid){
        QueryWrapper<SysInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userid);
        sysInformationMapper.delete(queryWrapper);
        return true;
    }


    /**
     * 查询所有用户,分页,传入需要的页码current和每页显示的数量size
     */
    public List<SysUser> selectAllSysUserPage(long current,long size)
    {
        return sysUserMapper.selectPage(new Page<SysUser>(current,size),null).getRecords();
    }
    /**
     * 按真实姓名查询用户
     * 分页
     * 输入真实姓名
     * @return SysUser
     */
    public List<SysUser> selectUserInRealNamePage(long current,long size,String real_name){
        /**
         * 构造条件q1
         * q1：按真实姓名来查找
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.in("real_name",real_name);
        return sysUserMapper.selectPage(new Page<SysUser>(current,size),q1).getRecords();
    }


    /**
     * 按真实姓名查询用户总数
     * 分页
     * 输入真实姓名
     * @return SysUser
     */
    public long countUserInRealNamePage(String real_name){
        /**
         * 构造条件q1
         * q1：按真实姓名来查找
         */
        QueryWrapper<SysUser> q1 = new QueryWrapper<>();
        q1.in("real_name",real_name);
        return sysUserMapper.selectCount(q1);
    }

    /**
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     * 已整理
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础查询
     * ===================================================================================================
     */

    /**
     * 查询用户信息
     */
    public List<SysUser> selectSysUser(String id,String user_name,String real_name,String hash_password,String dept,String del_flag){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_name),"user_name",user_name);
        queryWrapper.in(!StringUtils.isEmpty(real_name),"real_name",real_name);
        queryWrapper.in(!StringUtils.isEmpty(hash_password),"hash_password",hash_password);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        return sysUserMapper.selectList(queryWrapper);
    }
    /**
     * 查询用户信息数量
     */
    public long countSysUser(String id,String user_name,String real_name,String hash_password,String dept,String del_flag){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_name),"user_name",user_name);
        queryWrapper.in(!StringUtils.isEmpty(real_name),"real_name",real_name);
        queryWrapper.in(!StringUtils.isEmpty(hash_password),"hash_password",hash_password);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        return sysUserMapper.selectCount(queryWrapper);
    }
    /**
     * 查询用户信息
     * 分页
     */
    public List<SysUser> selectSysUserPage(String id,String user_name,String real_name,String hash_password,String dept,String del_flag,long current,long size){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_name),"user_name",user_name);
        queryWrapper.in(!StringUtils.isEmpty(real_name),"real_name",real_name);
        queryWrapper.in(!StringUtils.isEmpty(hash_password),"hash_password",hash_password);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        return sysUserMapper.selectPage(new Page<SysUser>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询用户信息
     * 列表查询
     */
    public List<SysUser> selectSysUserList(List<String> id,List<String> user_name,List<String> real_name,List<String> hash_password,List<String> dept,List<String> del_flag){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_name!=null,"user_name",user_name);
        queryWrapper.in(real_name!=null,"real_name",real_name);
        queryWrapper.in(hash_password!=null,"hash_password",hash_password);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        return sysUserMapper.selectList(queryWrapper);
    }
    /**
     * 查询用户信息数量
     * 列表查询
     */
    public long countSysUserList(List<String> id, List<String> user_name, List<String> real_name, List<String> hash_password, List<String> dept, List<String> del_flag){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_name!=null,"user_name",user_name);
        queryWrapper.in(real_name!=null,"real_name",real_name);
        queryWrapper.in(hash_password!=null,"hash_password",hash_password);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        return sysUserMapper.selectCount(queryWrapper);
    }
    /**
     * 查询用户信息
     * 列表查询
     * 分页
     */
    public List<SysUser> selectSysUserListPage(List<String> id,List<String> user_name,List<String> real_name,List<String> hash_password,List<String> dept,List<String> del_flag,long current,long size){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_name!=null,"user_name",user_name);
        queryWrapper.in(real_name!=null,"real_name",real_name);
        queryWrapper.in(hash_password!=null,"hash_password",hash_password);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        return sysUserMapper.selectPage(new Page<SysUser>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询用户角色信息
     */
    public List<SysUserRoleRel> selectSysUserRole(String id,String user_id,String role_id){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        return sysUserRoleRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询用户角色信息数量
     */
    public long countSysUserRole(String id,String user_id,String role_id){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        return sysUserRoleRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询用户角色信息
     * 分页
     */
    public List<SysUserRoleRel> selectSysUserRolePage(String id,String user_id,String role_id,long current,long size){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        return sysUserRoleRelMapper.selectPage(new Page<SysUserRoleRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询用户角色信息
     * 列表查询
     */
    public List<SysUserRoleRel> selectSysUserRoleList(List<String> id,List<String> user_id,List<String> role_id){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        return sysUserRoleRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询用户角色信息数量
     * 列表查询
     */
    public long countSysUserRoleList(List<String> id,List<String> user_id,List<String> role_id){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        return sysUserRoleRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询用户角色信息
     * 列表查询
     * 分页
     */
    public List<SysUserRoleRel> selectSysUserRoleListPage(List<String> id,List<String> user_id,List<String> role_id,long current,long size){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        return sysUserRoleRelMapper.selectPage(new Page<SysUserRoleRel>(current,size),queryWrapper).getRecords();
    }

    /**
     * ===================================================================================================
     * 基础删除
     * ===================================================================================================
     */

    /**
     * 查询角色信息数量
     */
    public boolean delSysUser(String id,String user_name,String real_name,String hash_password,String dept,String del_flag){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_name),"user_name",user_name);
        queryWrapper.in(!StringUtils.isEmpty(real_name),"real_name",real_name);
        queryWrapper.in(!StringUtils.isEmpty(hash_password),"hash_password",hash_password);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(del_flag),"del_flag",del_flag);
        sysUserMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 查询角色信息数量
     * 列表查询
     */
    public boolean delSysUserList(List<String> id, List<String> user_name, List<String> real_name, List<String> hash_password, List<String> dept, List<String> del_flag){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_name!=null,"user_name",user_name);
        queryWrapper.in(real_name!=null,"real_name",real_name);
        queryWrapper.in(hash_password!=null,"hash_password",hash_password);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(del_flag!=null,"del_flag",del_flag);
        sysUserMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 查询用户角色信息数量
     */
    public boolean delSysUserRole(String id,String user_id,String role_id){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(user_id),"user_id",user_id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        sysUserRoleRelMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 查询用户角色信息数量
     * 列表查询
     */
    public boolean delSysUserRoleList(List<String> id,List<String> user_id,List<String> role_id){
        QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(user_id!=null,"user_id",user_id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        sysUserRoleRelMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 逻辑删除
     * true:不删除
     * flase：删除
     */
    public boolean logicDel(String id,boolean b){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        if(b){
            updateWrapper.set("del_flag","1");
        }else {
            updateWrapper.set("del_flag","0");
        }
        sysUserMapper.update(null,updateWrapper);
        return true;
    }
    /**
     * 列表
     * 逻辑删除
     * true:不删除
     * flase：删除
     */
    public boolean logicDelList(List<String> id,boolean b){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        if(b){
            updateWrapper.set("del_flag","1");
        }else {
            updateWrapper.set("del_flag","0");
        }
        sysUserMapper.update(null,updateWrapper);
        return true;
    }

    /**
     * ===================================================================================================
     * 基础更新
     * ===================================================================================================
     */

    /**
     * 修改密码
     */
    public boolean updatePwd(String userid,String pwd){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("user_name",userid);
        updateWrapper.set("hash_password",SysService.encrypt(pwd));
        sysUserMapper.update(null,updateWrapper);
        return true;
    }
    /**
     * 更新用户
     */
    public boolean updateSysUser(SysUser sysUser){
//        try {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",sysUser.getId());
        sysUserMapper.update(sysUser,queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    /**
     * ===================================================================================================
     * 基础新增
     * ===================================================================================================
     */

    /**
     * 添加用户
     * 需要随便向pojo中输入一个id
     */
    public String insertSysUser(SysUser sysUser){
//        try {
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysUser.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysUserMapper.insert(sysUser);
//        }catch (Exception e){
//            return sysUser.getId();
//        }
        return sysUser.getId();
    }
    /**
     * 添加用户角色
     * 需要随便向pojo中输入一个id
     */
    public String insertSysUserRoleRel(String user_id,String role_id){
//        try {
        SysUserRoleRel sysUserRoleRel = new SysUserRoleRel();
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysUserRoleRel.setUserId(user_id);
        sysUserRoleRel.setRoleId(role_id);
        sysUserRoleRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysUserRoleRelMapper.insert(sysUserRoleRel);
//        }catch (Exception e){
//            return sysUserRoleRel.getId();
//        }
        return sysUserRoleRel.getId();
    }
    /**
     * 批量
     * 添加用户
     * 需要随便向pojo中输入一个id
     */
    public int insertSysUserList(List<SysUser> sysUsers){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysUsers.size();i++){
            sysUsers.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysUserMapper.insertBatchSomeColumn(sysUsers);
    }
    /**
     * 批量
     * 添加用户角色
     * 需要随便向pojo中输入一个id
     */
    public int insertSysUserRoleRelList(List<SysUserRoleRel> sysUserRoleRels){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysUserRoleRels.size();i++){
            sysUserRoleRels.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysUserRoleRelMapper.insertBatchSomeColumn(sysUserRoleRels);
    }
    /**
     * ===================================================================================================
     * 级联删除
     * ===================================================================================================
     */

    @Autowired
    Dept dept;

    /**
     * 级联删除用户表
     * @param user_id
     * @return
     */
    public boolean delCascadeUser(String user_id){
        information.delPtyStu(null,null,null,null,user_id);
        information.delPtyTeacher(null,null,null,null,user_id);
        dept.delSysDeptUser(null,null,user_id);
        delSysUserRole(user_id,null,null);
        delSysUser(user_id,null,null,null,null,null);
        return true;
    }
    /**
     * 级联删除用户表
     * @param user_id
     * @return
     */
    public boolean delCascadeUserList(List<String> user_id){
        information.delPtyStuList(null,null,null,null,user_id);
        information.delPtyTeacherList(null,null,null,null,user_id);
        dept.delSysDeptUserList(null,null,user_id);
        delSysUserRoleList(user_id,null,null);
        delSysUserList(user_id,null,null,null,null,null);
        return true;
    }

    /**
     * ===================================================================================================
     * 复杂查询
     * ===================================================================================================
     */

    /**
     * 分页
     * 获取当前班级的学生列表
     * @param dept_id
     * @param current
     * @param size
     * @return
     */
    public List<SysUser> selectUserListByDeptPage(String dept_id,long current,long size){
        List<SysUser> list = sysUserMapper.selectJoinPage(new Page<>(current, size),SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .eq(SysDeptUserRel::getDeptId,dept_id)).getRecords();
        return list;
    }

    /**
     * 分页
     * 组织筛选(教师学生都用这个)
     * @param dept_name
     * @param current
     * @param size
     * @return
     */
    public List<SysUser> selectUserListByDeptNamePage(String dept_name,long current,long size){
        List<SysUser> list = sysUserMapper.selectJoinPage(new Page<>(current, size),SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .leftJoin(SysDept.class, SysDept::getId,SysDeptUserRel::getDeptId)
                        .eq(SysDept::getDeptName,dept_name)).getRecords();
        return list;
    }
    /**
     * 分页
     * 查询学生通过组织筛选+真实姓名(学生老师共用)
     * @param dept_name
     * @param current
     * @param size
     * @return
     */
    public List<SysUser> selectUserListByDeptNamePage(String dept_name,String real_name,long current,long size){
        List<SysUser> list = sysUserMapper.selectJoinPage(new Page<>(current, size),SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .leftJoin(SysDept.class, SysDept::getId,SysDeptUserRel::getDeptId)
                        .eq(SysDept::getDeptName,dept_name)
                        .eq(SysUser::getRealName,real_name)).getRecords();
        return list;
    }
    /**
     * 获取当前组织的学生列表
     * @param dept_id
     * @return
     */
    public List<SysUser> selectUserListByDept(String dept_id){
        List<SysUser> list = sysUserMapper.selectJoinList(SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .eq(SysDeptUserRel::getDeptId,dept_id));
        return list;
    }
    /**
     * 组织筛选(教师学生都用这个)
     * @param dept_name
     * @return
     */
    public List<SysUser> selectUserListByDeptName(String dept_name){
        List<SysUser> list = sysUserMapper.selectJoinList(SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .leftJoin(SysDept.class, SysDept::getId,SysDeptUserRel::getDeptId)
                        .eq(SysDept::getDeptName,dept_name));
        return list;
    }
    /**
     * 查询学生通过组织筛选+真实姓名(学生老师共用)
     * @param dept_name
     * @return
     */
    public List<SysUser> selectUserListByDeptName(String dept_name,String real_name) {
        List<SysUser> list = sysUserMapper.selectJoinList(SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .leftJoin(SysDept.class, SysDept::getId,SysDeptUserRel::getDeptId)
                        .eq(SysDept::getDeptName,dept_name)
                        .eq(SysUser::getRealName,real_name));
        return list;
    }
    /**
     * 查询学生通过组织筛选+真实姓名(学生老师共用)
     * @param dept_id
     * @return
     */
    public List<SysUser> selectUserListByDeptIdAndName(String dept_id,String real_name) {
        List<SysUser> list = sysUserMapper.selectJoinList(SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .eq(SysDeptUserRel::getDeptId,dept_id)
                        .eq(SysUser::getRealName,real_name));
        return list;
    }
    /**
     * 查询学生通过组织筛选+真实姓名(学生老师共用)
     * @param dept_id
     * @return
     */
    public List<SysUser> selectUserListByDeptIdAndNamePage(String dept_id,String real_name,long current,long size) {
        List<SysUser> list = sysUserMapper.selectJoinPage(new Page<>(current, size),SysUser.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .eq(SysDeptUserRel::getDeptId,dept_id)
                        .eq(SysUser::getRealName,real_name)).getRecords();
        return list;
    }
    /**
     * 总数
     * 查询学生通过组织筛选+真实姓名(学生老师共用)
     * @param dept_id
     * @return
     */
    public long countUserListByDeptIdAndNamePage(String dept_id,String real_name) {
        long list = sysUserMapper.selectJoinCount(
                new MPJLambdaWrapper<>()
                        .selectAll(SysUser.class)
                        .leftJoin(SysDeptUserRel.class, SysDeptUserRel::getUserId,SysUser::getId)
                        .eq(SysDeptUserRel::getDeptId,dept_id)
                        .eq(SysUser::getRealName,real_name));
        return list;
    }
    /**
     * ===================================================================================================
     * 视图查询
     * ===================================================================================================
     */

    @Autowired
    private TeacherInfMapper teacherInfMapper;
    @Autowired
    private StudentInfMapper studentInfMapper;
    /**
     * 学生用户视图查询
     */
    public List<StudentInf> selectStudentInf(String id,String userName,String realName,String dept,String delFlag,String sex,String openId,String phone,String parentName,String deptName){
        QueryWrapper<StudentInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(userName),"user_name",userName);
        queryWrapper.in(!StringUtils.isEmpty(realName),"real_name",realName);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(delFlag),"del_flag",delFlag);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(openId),"open_id",openId);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(parentName),"parent_name",parentName);
        queryWrapper.in(!StringUtils.isEmpty(deptName),"dept_name",deptName);
        return studentInfMapper.selectList(queryWrapper);
    }
    /**
     * 教师用户视图查询
     */
    public List<TeacherInf> selectTeacherInf(String id,String userName,String realName,String dept,String delFlag,String sex,String openId,String phone,String deptName){
        QueryWrapper<TeacherInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(userName),"user_name",userName);
        queryWrapper.in(!StringUtils.isEmpty(realName),"real_name",realName);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(delFlag),"del_flag",delFlag);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(openId),"open_id",openId);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(deptName),"dept_name",deptName);
        return teacherInfMapper.selectList(queryWrapper);
    }
    /**
     * 列表
     * 学生用户视图查询
     */
    public List<StudentInf> selectStudentInfList(List<String> id,List<String> userName,List<String> realName,List<String> dept,List<String> delFlag,List<String> sex,List<String> openId,List<String> phone,List<String> parentName,List<String> deptName){
        QueryWrapper<StudentInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(userName!=null,"user_name",userName);
        queryWrapper.in(realName!=null,"real_name",realName);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(delFlag!=null,"del_flag",delFlag);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(openId!=null,"open_id",openId);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(parentName!=null,"parent_name",parentName);
        queryWrapper.in(deptName!=null,"dept_name",deptName);
        return studentInfMapper.selectList(queryWrapper);
    }
    /**
     * 列表
     * 教师用户视图查询
     */
    public List<TeacherInf> selectTeacherInfList(List<String> id,List<String> userName,List<String> realName,List<String> dept,List<String> delFlag,List<String> sex,List<String> openId,List<String> phone,List<String> deptName){
        QueryWrapper<TeacherInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(userName!=null,"user_name",userName);
        queryWrapper.in(realName!=null,"real_name",realName);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(delFlag!=null,"del_flag",delFlag);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(openId!=null,"open_id",openId);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(deptName!=null,"dept_name",deptName);
        return teacherInfMapper.selectList(queryWrapper);
    }
    /**
     * 分页
     * 学生用户视图查询
     */
    public List<StudentInf> selectStudentInfPage(String id,String userName,String realName,String dept,String delFlag,String sex,String openId,String phone,String parentName,String deptName,long current,long size){
        QueryWrapper<StudentInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(userName),"user_name",userName);
        queryWrapper.in(!StringUtils.isEmpty(realName),"real_name",realName);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(delFlag),"del_flag",delFlag);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(openId),"open_id",openId);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(parentName),"parent_name",parentName);
        queryWrapper.in(!StringUtils.isEmpty(deptName),"dept_name",deptName);
        return studentInfMapper.selectPage(new Page<StudentInf>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页
     * 教师用户视图查询
     */
    public List<TeacherInf> selectTeacherInfPage(String id,String userName,String realName,String dept,String delFlag,String sex,String openId,String phone,String deptName,long current,long size){
        QueryWrapper<TeacherInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(userName),"user_name",userName);
        queryWrapper.in(!StringUtils.isEmpty(realName),"real_name",realName);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(delFlag),"del_flag",delFlag);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(openId),"open_id",openId);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(deptName),"dept_name",deptName);
        return teacherInfMapper.selectPage(new Page<TeacherInf>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页
     * 列表
     * 学生用户视图查询
     */
    public List<StudentInf> selectStudentInfListPage(List<String> id,List<String> userName,List<String> realName,List<String> dept,List<String> delFlag,List<String> sex,List<String> openId,List<String> phone,List<String> parentName,List<String> deptName,long current,long size){
        QueryWrapper<StudentInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(userName!=null,"user_name",userName);
        queryWrapper.in(realName!=null,"real_name",realName);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(delFlag!=null,"del_flag",delFlag);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(openId!=null,"open_id",openId);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(parentName!=null,"parent_name",parentName);
        queryWrapper.in(deptName!=null,"dept_name",deptName);
        return studentInfMapper.selectPage(new Page<StudentInf>(current,size),queryWrapper).getRecords();
    }
    /**
     * 分页
     * 列表
     * 教师用户视图查询
     */
    public List<TeacherInf> selectTeacherInfListPage(List<String> id,List<String> userName,List<String> realName,List<String> dept,List<String> delFlag,List<String> sex,List<String> openId,List<String> phone,List<String> deptName,long current,long size){
        QueryWrapper<TeacherInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(userName!=null,"user_name",userName);
        queryWrapper.in(realName!=null,"real_name",realName);
        queryWrapper.in(dept!=null,"dept",dept);
        queryWrapper.in(delFlag!=null,"del_flag",delFlag);
        queryWrapper.in(sex!=null,"sex",sex);
        queryWrapper.in(openId!=null,"open_id",openId);
        queryWrapper.in(phone!=null,"phone",phone);
        queryWrapper.in(deptName!=null,"dept_name",deptName);
        return teacherInfMapper.selectPage(new Page<TeacherInf>(current,size),queryWrapper).getRecords();
    }
    /**
     * 总数
     * 学生用户视图查询
     */
    public long countStudentInf(String id,String userName,String realName,String dept,String delFlag,String sex,String openId,String phone,String parentName,String deptName){
        QueryWrapper<StudentInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(userName),"user_name",userName);
        queryWrapper.in(!StringUtils.isEmpty(realName),"real_name",realName);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(delFlag),"del_flag",delFlag);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(openId),"open_id",openId);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(parentName),"parent_name",parentName);
        queryWrapper.in(!StringUtils.isEmpty(deptName),"dept_name",deptName);
        return studentInfMapper.selectCount(queryWrapper);
    }
    /**
     * 总数
     * 教师用户视图查询
     */
    public long countTeacherInf(String id,String userName,String realName,String dept,String delFlag,String sex,String openId,String phone,String deptName){
        QueryWrapper<TeacherInf> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(userName),"user_name",userName);
        queryWrapper.in(!StringUtils.isEmpty(realName),"real_name",realName);
        queryWrapper.in(!StringUtils.isEmpty(dept),"dept",dept);
        queryWrapper.in(!StringUtils.isEmpty(delFlag),"del_flag",delFlag);
        queryWrapper.in(!StringUtils.isEmpty(sex),"sex",sex);
        queryWrapper.in(!StringUtils.isEmpty(openId),"open_id",openId);
        queryWrapper.in(!StringUtils.isEmpty(phone),"phone",phone);
        queryWrapper.in(!StringUtils.isEmpty(deptName),"dept_name",deptName);
        return teacherInfMapper.selectCount(queryWrapper);
    }
}
